﻿using System;
using Medicalsystem.MCP.FormDesigner.Common;
using Medicalsystem.MCP.FormDesigner.Interface;

namespace Medicalsystem.MCP.FormDesigner.Property
{
    /// <summary>
    /// 控件属性的XML转换
    /// 继承 ICtrlUI 接口的控件使用
    /// </summary>
    public class CtrlUIPropertyXML : CtrlBasePropertyXML
    {
        /// <summary>
        /// 控件基本属性的XML转换
        /// </summary>
        /// <param name="C"></param>
        public CtrlUIPropertyXML(System.Windows.Forms.Control C) : base(C) { }

        #region IXMLConvertor 成员

        public override string ToXML()
        {
            return "<ShowText>" + ESCEncrypt.escape(((ICtrlUI)_control).ShowText) + "</ShowText>" +
                      "<Height>" + _control.Height.ToString() + "</Height>" +
                      "<Width>" + _control.Width.ToString() + "</Width>" +
                      "<Top>" + _control.Top.ToString() + "</Top>" +
                      "<Left>" + _control.Left.ToString() + "</Left>" +
                      "<TabIndex>" + _control.TabIndex.ToString() + "</TabIndex>" +
                      "<BackColor>" + _control.BackColor.ToArgb().ToString() + "</BackColor>" +
                      "<ForeColor>" + _control.ForeColor.ToArgb().ToString() + "</ForeColor>" +
                      "<Font>" + FontSerializer.FromFont(_control.Font).ToXml() + "</Font>" +
                      "<Anchor>" + ((int)_control.Anchor).ToString() + "</Anchor>" +
                      "<Dock>" + ((int)_control.Dock).ToString() + "</Dock>" +
                      "<VisibleZ>" + BoolToString(((ICtrlUI)_control).VisibleZ) + "</VisibleZ>" +
                      "<BorderStyle>" + ((int)((ICtrlUI)_control).BorderStyleZ).ToString() + "</BorderStyle>" +
                      "<Base>" + base.ToXML() + "</Base>";
        }

        public override void FromXML(System.Xml.XmlNodeList xns)
        {
            if (xns != null && xns.Count > 0)
            {
                foreach (System.Xml.XmlNode XN in xns)
                {
                    switch (XN.Name.ToLower())
                    {
                        case "showtext":
                            ((ICtrlUI)_control).ShowText = ESCEncrypt.unescape(XN.InnerText);
                            break;

                        case "height":
                            _control.Height = Convert.ToInt32(XN.InnerText);
                            break;

                        case "width":
                            _control.Width = Convert.ToInt32(XN.InnerText);
                            break;

                        case "top":
                            _control.Top = Convert.ToInt32(XN.InnerText);
                            break;

                        case "left":
                            _control.Left = Convert.ToInt32(XN.InnerText);
                            break;

                        case "tabindex":
                            _control.TabIndex = Convert.ToInt32(XN.InnerText);
                            break;

                        case "backcolor":
                            _control.BackColor = System.Drawing.Color.FromArgb(Convert.ToInt32(XN.InnerText));
                            break;

                        case "forecolor":
                            _control.ForeColor = System.Drawing.Color.FromArgb(Convert.ToInt32(XN.InnerText));
                            break;

                        case "font":
                            _control.Font = FontSerializer.FromXml(XN.OuterXml).ToFont();
                            break;

                        case "anchor":
                            _control.Anchor = (System.Windows.Forms.AnchorStyles)Convert.ToInt32(XN.InnerText);
                            break;

                        case "dock":
                            _control.Dock = (System.Windows.Forms.DockStyle)Convert.ToInt32(XN.InnerText);
                            break;

                        case "visiblez":
                            if (((ICtrlUI)_control).DesignModeState == EDesignModeState.Design)
                            {
                                ((ICtrlUI)_control).VisibleZ = StringToBool(XN.InnerText);
                            }
                            else
                            {
                                _control.Visible = StringToBool(XN.InnerText);
                            }
                            break;

                        case "borderstyle":
                            ((ICtrlUI)_control).BorderStyleZ = (EBorderStyle)Convert.ToInt32(XN.InnerText);
                            break;

                        case "base":
                            base.FromXML(XN.ChildNodes);
                            break;
                        default:
                            break;
                    }
                }
            }//end if
        }

        #endregion IXMLConvertor 成员
    }
}